<?php
Class Controller_Storage_Purchase extends Controller_Abstract
{
	function actionList()
	{
		$this->_view['title'] = '采购入库单列表';
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$url_arr = Helper_BSS_Normal::buildCondition();
		if (empty($url_arr['purchase_order_status']))
		{
			$url_arr['purchase_order_status'] = array(
					Q::ini('custom_flag/purchase_order_status/arrived/value') => Q::ini('custom_flag/purchase_order_status/arrived/value'),
					Q::ini('custom_flag/purchase_order_status/storaged/value') => Q::ini('custom_flag/purchase_order_status/storaged/value'),
					Q::ini('custom_flag/purchase_order_status/partial_storaged/value') => Q::ini('custom_flag/purchase_order_status/partial_storaged/value')
			);
		}
		$url_arr['type'] = Q::ini('custom_flag/purchase_order_type/order/value');
		$warehouse = Warehouse::find('type=? and status=?', 'gz', true)->asArray()->getAll();
		$warehouse_id = array();
		foreach ($warehouse as $w)
		{
			$warehouse_id[] = $w['id'];
		}
		$url_arr['warehouse_id'] = empty($url_arr['warehouse_id']) ? $warehouse_id : $url_arr['warehouse_id'];
		$this->_view['warehouse'] = $warehouse;
		$result = Purchase_Order::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$purchase_order = $result['data'];
			$page = new Helper_BSS_Page($result['pagination'], url('Storage::Purchase/List', $url_arr));
			$this->_view['purchase_order'] = $purchase_order;
			$this->_view['page'] = $page->getPage();;
		}
	}
	
	function actionAuditList()
	{
		$this->_view['title'] = '确认已到仓列表';
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$url_arr['type'] = Q::ini('custom_flag/purchase_order_type/order/value');
		$url_arr['audit_status'] = true;
		$warehouse = Warehouse::find('type=? and status=?', 'gz', true)->asArray()->getAll();
		$warehouse_id = array();
		foreach ($warehouse as $w)
		{
			$warehouse_id[] = $w['id'];
		}
		$url_arr['warehouse_id'] = empty($url_arr['warehouse_id']) ? $warehouse_id : $url_arr['warehouse_id'];;
		$this->_view['warehouse'] = $warehouse;
		$result = Purchase_Order::Search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$purchase_order = $result['data'];
			$page = new Helper_BSS_Page($result['pagination'], url('Storage::Purchase/AuditList', $url_arr));
			$this->_view['page'] = $page->getPage();
			$this->_view['purchase_order'] = $purchase_order;
		}
	}
	
	function actionWarehouseAudit()
	{
		$this->_view['title'] = '已到仓审核';
		if (isset($_GET['id']))
		{
			$result = Purchase_Order::auditWarehoused($_GET['id']);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirect($_SERVER['HTTP_REFERER']);
			}
			else
			{
				return $this->_redirectMessage('审核失败', $result['message'], $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionDetail()
	{
		$this->_view['title'] = '详细';
		$material = Product_Material::find()->asArray()->getAll();
		$this->_view['material'] = $material;
		$url_arr = Helper_BSS_Normal::buildCondition();
		if ($url_arr)
		{
			$purchase_order = Purchase_Order::find('purchase_order.id =?', $url_arr['id'])
				->joinLeft('user', 'name as purchase_user_name', 'user.id = purchase_order.purchase_user_id')
				->asArray()->getOne();
			$where = 'purchase_order_item.purchase_order_id=' . $url_arr['id'];
			if (isset($url_arr['show_all']) && $url_arr['show_all'] == 0)
			{
				$where .= ' and purchase_order_item.actual_quantity>0';
			}
			$item = Purchase_Order_Item::find($where)
				->joinLeft('product_basic', 'number as product_number, name as product_name, weight, declared_value, declared_name_cn, declared_name_en', 'product_basic.id=purchase_order_item.product_basic_id')
				->joinLeft('product_warehouse', 'id as product_warehouse_id, actual_quantity as product_warehouse_actual_quantity, shelf_position, product_feature, tag', 'product_warehouse.product_basic_id=product_basic.id and product_warehouse.warehouse_id=' . $purchase_order['warehouse_id'])
				->joinLeft('product_material', 'id as product_material_id, number as material_number, name as material_name', 'product_material.id=product_warehouse.product_material_id')
				->order('product_basic.number asc')->asArray()->getAll();
			Helper_BSS_Normal::getProductImgUrl($item, 'product_number');
			$this->_view['purchase_order'] = $purchase_order;
			$this->_view['item'] = $item;
		}
	}
	
	function actionAuditSingle()
	{
		$this->_view['title'] = '采购单审核入库（单个操作）';
		if (isset($_GET['id']))
		{
			$result = Purchase_Order::auditStorageSingle($_GET['id']);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirect($_SERVER['HTTP_REFERER']);
			}
			else
			{
				return $this->_redirectMessage('入库失败', $result['message'], $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionAudit()
	{
		$this->_view['title'] = '采购单审核入库';
		$ids = explode(',', $_GET['ids']);
		if ($ids)
		{
			$judge_result = Purchase_Order::auditStorage($ids);
			if ($judge_result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('审核成功，已经入库', '正在返回..', $_SERVER['HTTP_REFERER'], $judge_result['ack']);
			}
			else
			{
				return $this->_redirectMessage('部分审核失败', $judge_result['message'], $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionPrintReceivingTable()
	{
		$this->_view['title'] = '打印收货单';
		$ids = explode(',', $_GET['ids']);
		if ($ids)
		{
			$purchase_order = Purchase_Order::find('purchase_order.id in (?)', $ids)
				->joinLeft('user as a', 'name as make_user_name', 'a.id = purchase_order.make_user_id')
				->joinLeft('user as b', 'name as purchase_user_name', 'b.id = purchase_order.purchase_user_id')
				->joinLeft('user as c', 'name as purchase_audit_user_name', 'c.id = purchase_order.purchase_audit_user_id')
				->joinLeft('user as d', 'name as storage_audit_user_name', 'd.id = purchase_order.storage_audit_user_id')
				->joinLeft('user as e', 'name as complete_user_name', 'e.id = purchase_order.complete_user_id')
				->asArray()->getAll();
			foreach ($purchase_order as &$p)
			{
				$item = Purchase_Order_Item::find('purchase_order_item.purchase_order_id=? and purchase_order_item.actual_quantity>0', $p['id'])
					->joinLeft('product_basic', 'number as product_number, name as product_name', 'product_basic.id=purchase_order_item.product_basic_id')
					->joinLeft('product_warehouse', 'shelf_position, sold_quantity', 'product_warehouse.product_basic_id=product_basic.id and product_warehouse.warehouse_id=' . $p['warehouse_id'])
					->order('product_basic.number asc')->asArray()->getAll();
				$p['item'] = $item;
			}
			unset($p);
			$this->_view['purchase_order'] = $purchase_order;
		}
	}
	
	function actionPrintProductBarcode()
	{
		$this->_view['title'] = '打印产品条形码';
		if (!empty($_GET['ids']))
		{
			$ids = explode(',', $_GET['ids']);
			$purchase_order = Purchase_Order::find('id in (?)', $ids)->asArray()->getAll();
			$item = array();
			foreach ($purchase_order as $p)
			{
				$purchase_order_item = Purchase_order_item::find('purchase_order_item.purchase_order_id=?', $p['id'])
					->joinLeft('product_basic', '', 'product_basic.id=purchase_order_item.product_basic_id')
					->joinLeft('product_warehouse', '', 'product_warehouse.product_basic_id=product_basic.id and product_warehouse.warehouse_id=' . $p['warehouse_id'])
					->joinLeft('product_material','', 'product_material.id=product_warehouse.product_material_id')
					->setColumns('product_basic.number, product_warehouse.shelf_position, product_warehouse.product_feature, purchase_order_item.actual_quantity as quantity, product_material.number as material_number')
					->asArray()->getAll();
				$item = array_merge($purchase_order_item, $item);
			}
			$item = Helper_BSS_Array::SortByValue($item, 'number', 'asc');
			OLM_Barcode::productNumber($item);
			exit;
		}
		if (!empty($_GET['pid']) && !empty($_GET['qty']))
		{
			$product = Product_Warehouse::find('product_warehouse.id=?', $_GET['pid'])
				->joinLeft('product_basic', '', 'product_basic.id=product_warehouse.product_basic_id')
				->joinLeft('product_material','', 'product_material.id=product_warehouse.product_material_id')
				->setColumns('product_basic.number, product_warehouse.shelf_position, product_warehouse.product_feature, product_material.number as material_number')
				->asArray()->getOne();
			$product['quantity'] = $_GET['qty'];
			OLM_Barcode::productNumber(array($product));
			exit;
		}
	}
	
	function actionReturnPurchased()
	{
		$this->_view['title'] = '返审已采购';
		$ids = explode(',', $_GET['ids']);
		if ($ids)
		{
			$result = Purchase_Order::returnPurchased($ids);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('成功返回已采购', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('部分返回已采购失败' ,$result['message'], $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionDrawbackCreate()
	{
		$this->_view['title'] = '采购退货单创建';
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$this->_view['warehouse'] = Warehouse::getActiveWarehouse();
	}
	
	function actionDrawbackCreateSave()
	{
		$this->_view['title'] = '采购退货单创建（保存）';
		if ($_POST)
		{
			$item = isset($_POST['item']) ? $_POST['item'] : array();
			$tmp = array();
			foreach ($item as $k => $i)
			{
				if (array_key_exists($i['product_basic_id'], $tmp))
				{
					$item[$tmp[$i['product_basic_id']]]['actual_quantity'] += $i['actual_quantity'];
					unset($item[$k]);
				}
				else
				{
					$tmp[$i['product_basic_id']] = $k;
				}
			}
			$drawback = array(
				'purchase_time' => $_POST['purchase_time'],
				'purchase_user_id' => $_POST['purchase_user_id'],
				'payment_type' => $_POST['purchase_order_payment_type'],
				'warehouse_id' => $_POST['warehouse_id'],
				'notes' => trim($_POST['notes']),
				'item' => $item
			);
			$result = Purchase_Order::createDrawback($drawback);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('保存成功', '正在返回..', url('Storage::Purchase/DrawbackList'), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('保存失败' ,$result['message'], $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionDelDrawback()
	{
		$this->_view['title'] = '采购退货单删除（操作）';
		$result = Purchase_Order::delDrawback($_GET['id']);
		if ($result['ack'] == SUCCESS)
		{
			return $this->_redirectMessage('删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
		}
		else
		{
			return $this->_redirectMessage('删除失败', $result['message'], $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionDrawbackList()
	{
		$this->_view['title'] = '采购退货单列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$this->_view['warehouse'] = Warehouse::getActiveWarehouse();
		$url_arr = Helper_BSS_Normal::buildCondition();
		$url_arr['type'] = Q::ini('custom_flag/purchase_order_type/drawback/value');
		$result = Purchase_Order::search($url_arr, $cp, $ps);
		if ($result['ack'] ==SUCCESS)
		{
			$purchase_order = $result['data'];
			$this->_view['purchase_order'] = $purchase_order;
			$page = new Helper_BSS_Page($result['pagination'], url('Storage::Purchase/DrawbackList', $url_arr));
			$this->_view['page'] = $page->getPage();
		}
	}
	
	function actionDrawbackEdit()
	{
		$this->_view['title'] = '采购退货单编辑';
		if ( ! empty($_GET['id']))
		{
			$purchase_order = Purchase_Order::find('purchase_order.id=? and purchase_order.type=? and purchase_order.status=?', $_GET['id'], Q::ini('custom_flag/purchase_order_type/drawback/value'), Q::ini('custom_flag/purchase_order_status/new/value'))
				->joinLeft('warehouse', 'warehouse.name as warehouse_name', 'warehouse.id=purchase_order.warehouse_id')
				->asArray()->getOne();
			if ($purchase_order)
			{
				$item = Purchase_Order_Item::find('purchase_order_item.purchase_order_id=?', $purchase_order['id'])
					->joinLeft('product_warehouse', 'available_quantity', 'product_warehouse.product_basic_id=purchase_order_item.product_basic_id and product_warehouse.warehouse_id=' . $purchase_order['warehouse_id'])
					->joinLeft('product_basic', 'number as product_number, name as product_name', 'product_basic.id=purchase_order_item.product_basic_id')
					->joinLeft('product_provider', 'name as actual_provider_name', 'product_provider.id=purchase_order_item.actual_provider_id')
					->asArray()->getAll();
				$this->_view['purchase_order'] = $purchase_order;
				$this->_view['item'] = $item;
				$user = Purchase_Order::getUser();
				$this->_view['user'] = $user['data'];
			}
			else
			{
				return $this->_redirectMessage('发生错误', '正在返回..', url('Storage::Purchase/DrawbackList'), FAILURE);
			}
		}
		else
		{
			return $this->_redirect(url('Storage::Purchase/DrawbackList'));
		}
	}
	
	function actionDrawbackEditSave()
	{
		$this->_view['title'] = '采购退货单编辑（保存）';
		if ($_POST)
		{
			$item = isset($_POST['item']) ? $_POST['item'] : array();
			$tmp = array();
			foreach ($item as $k => $i)
			{
				if (array_key_exists($i['product_basic_id'], $tmp))
				{
					$item[$tmp[$i['product_basic_id']]]['actual_quantity'] += $i['actual_quantity'];
					unset($item[$k]);
				}
				else
				{
					$tmp[$i['product_basic_id']] = $k;
				}
			}
			$drawback = array(
				'id' => $_POST['id'],
				'purchase_time' => $_POST['purchase_time'],	
				'purchase_user_id' => $_POST['purchase_user_id'],
				'payment_type' => $_POST['purchase_order_payment_type'],
				'notes' => trim($_POST['notes']),
				'item' => $item
			);
			$result = Purchase_Order::editDrawback($drawback);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('保存成功', '正在返回..', url('Storage::Purchase/DrawbackList'), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('保存失败', $result['message'], $_POST['history'], FAILURE);
			}
		}
	}
	
	function actionDrawbackAudit()
	{
		$this->_view['title'] = '采购退货单审核';
		$ids = explode(',', $_GET['ids']);
		if ($ids)
		{
			$result = Purchase_Order::auditDrawbackStorage($ids);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('审核成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('审核失败', $result['message'], url('Storage::Purchase/DrawbackList'), FAILURE);
			}
		}
	}
	
	function actionActualQuantitySave()
	{
		$this->_view['title'] = '采购实际数量保存（ajax）';
		$product_id = $_REQUEST['product_id'];
		$purchase_actual_quantity = trim($_REQUEST['purchase_actual_quantity']);
		$purchase_order_item_id = $_REQUEST['purchase_order_item_id'];
		$purchase_order_item = Purchase_Order_Item::find('id=?', $purchase_order_item_id)->getOne();
		if ($purchase_order_item && empty($purchase_order_item['audit_flag']))
		{
			if ($purchase_actual_quantity <= $purchase_order_item['budget_quantity'])
			{
				$purchase_order = Purchase_Order::find('id=?', $purchase_order_item['purchase_order_id'])->asArray()->getOne();
				if ($purchase_order['type'] != Q::ini('custom_flag/purchase_order_type/drawback/value'))
				{
					if ($purchase_order['status'] == Q::ini('custom_flag/purchase_order_status/arrived/value') || $purchase_order['status'] == Q::ini('custom_flag/purchase_order_status/partial_storaged/value'))
					{
						Product_Warehouse::purchaseActualQuantityDecrease(0, $purchase_order_item['product_basic_id'], $purchase_order['warehouse_id'], (int)$purchase_order_item['actual_quantity']);
						Product_Warehouse::purchaseActualQuantityIncrease(0, $purchase_order_item['product_basic_id'], $purchase_order['warehouse_id'], (int)$purchase_actual_quantity);
					}
					if ($purchase_order['status'] == Q::ini('custom_flag/purchase_order_status/purchased/value') || $purchase_order['status'] == Q::ini('custom_flag/purchase_order_status/arrived/value') || $purchase_order['status'] == Q::ini('custom_flag/purchase_order_status/partial_storaged/value'))
					{
						Product_Warehouse::purchaseQuantityDecrease(0, $purchase_order_item['product_basic_id'], $purchase_order['warehouse_id'], (int)$purchase_order_item['actual_quantity']);
					}
					$purchase_order_item->actual_quantity = (int)($purchase_actual_quantity);
					$purchase_order_item->save();
					Product_Warehouse::purchaseQuantityIncrease(0, $purchase_order_item['product_basic_id'], $purchase_order['warehouse_id'], (int)$purchase_order_item['actual_quantity']);
					Purchase_Order::updateOrder($purchase_order['id']);
				}
			}
			else
			{
				echo json_encode('实际数不能大于预计数！');
			}
		}
		else
		{
			echo json_encode('item不存在或者已审核！');
		}
		exit;
	}
	
	function actionProductInfoAjax()
	{
		$this->_view['title'] = '产品信息请求ajax';
		$warehouse_id = isset($_REQUEST['warehouse_id']) ? $_REQUEST['warehouse_id'] : Warehouse::getDefaultWarehouseId(); 
		$product_basic = Product_Warehouse::search(array('number_or_name' => $_REQUEST['q'], 'warehouse_id' => $warehouse_id), 0, 10);
		$result = array();
		foreach ($product_basic['data'] as $pb)
		{
			$result[] = array(
				0 => $pb['number'] . ' ' . $pb['product_basic_name'],
				'id' => $pb['product_basic_id'],
				'number' => $pb['number'],
				'purchase_price' => ($pb['last_purchase_price'] == 0) ? $pb['purchase_price'] : $pb['last_purchase_price'],
				'cost_price' => $pb['last_purchase_price'],
				'preferred_provider_id' => $pb['preferred_provider_id'],
				'preferred_provider_name' => $pb['preferred_provider_name'],
				'warning_quantity' => $pb['warning_quantity'],
				'safe_quantity' => $pb['safe_quantity'],
				'actual_quantity' => $pb['actual_quantity'],
				'sold_quantity' => $pb['sold_quantity'],
				'available_quantity' => $pb['available_quantity'],
				'purchase_budget_quantity' => $pb['purchase_budget_quantity'],
				'purchase_quantity' => $pb['purchase_quantity'],
				'sales_status' =>  Helper_BSS_Normal::getIniOne('product_sales_status','value',$pb['sales_status'],'name'),
				'purchase_status' => Helper_BSS_Normal::getIniOne('product_purchase_status','value',$pb['purchase_status'],'name'),
			);
		}
		echo json_encode($result);
		exit;
	}
	
	function actionProvider()
	{
		$this->_view['title'] = '供应商请求ajax';
		$q = $_REQUEST['q'];
		$provider = Product_Provider::find('id like ? or name like ?', $q.'%', '%'.$q.'%')->asArray()->getAll();
		$result = array();
		foreach ($provider as $p)
		{
			$result[] = array(
				0 => $p['id'] . ' ' . $p['name'],
				'id' => $p['id']
			);
		}
		echo json_encode($result);
		exit;
	}
	
	function actionActualQuantityEdit()
	{
		$this->_view['title'] = '实际数编辑';
		$material = Product_Material::find()->asArray()->getAll();
		$this->_view['material'] = $material;
		$url_arr = Helper_BSS_Normal::buildCondition();
		if ($url_arr)
		{
			$purchase_order = Purchase_Order::find('purchase_order.id =?', $url_arr['id'])
				->joinLeft('user', 'name as purchase_user_name', 'user.id = purchase_order.purchase_user_id')
				->asArray()->getOne();
			$where = 'purchase_order_item.purchase_order_id=' . $url_arr['id'];
			$item = Purchase_Order_Item::find($where)
				->joinLeft('product_basic', 'number as product_number, name as product_name, weight, declared_value, declared_name_cn, declared_name_en', 'product_basic.id=purchase_order_item.product_basic_id')
				->joinLeft('product_warehouse', 'id as product_warehouse_id, actual_quantity as product_warehouse_actual_quantity, shelf_position, product_feature, tag', 'product_warehouse.product_basic_id=product_basic.id and product_warehouse.warehouse_id=' . $purchase_order['warehouse_id'])
				->joinLeft('product_material', 'id as product_material_id, number as material_number, name as material_name', 'product_material.id=product_warehouse.product_material_id')
				->order('product_basic.number asc')->asArray()->getAll();
			Helper_BSS_Normal::getProductImgUrl($item, 'product_number');
			$this->_view['purchase_order'] = $purchase_order;
			$this->_view['item'] = $item;
		}
	}

	function actionActualQuantityEditSave()
	{
		$this->_view['title'] = '实际数编辑（保存）';
		if ($this->_context->isPost() && isset($_POST['purchase_order_id']) && isset($_POST['item']))
		{
			$result = Purchase_Order_Item::updateActual($_POST['item'], $_POST['purchase_order_id']);
			Purchase_Order::updateOrder($_POST['purchase_order_id']);
			return $this->_redirectMessage('保存成功', $result['message'], url('Storage::Purchase/Detail', array('id'=>$_REQUEST['purchase_order_id'], 'show_all'=>1, 'operate'=>1)), $result['ack']);
		}
		else
		{
			return $this->_redirectMessage('保存失败', '提交方式有误！', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionInfoEdit()
	{
		$this->_view['title'] = '信息编辑';
		$material = Product_Material::find()->asArray()->getAll();
		$this->_view['material'] = $material;
		$url_arr = Helper_BSS_Normal::buildCondition();
		if ($url_arr)
		{
			$purchase_order = Purchase_Order::find('purchase_order.id =?', $url_arr['id'])
				->joinLeft('user', 'name as purchase_user_name', 'user.id = purchase_order.purchase_user_id')
				->asArray()->getOne();
			$where = 'purchase_order_item.purchase_order_id=' . $url_arr['id'];
			$item = Purchase_Order_Item::find($where)
				->joinLeft('product_basic', 'number as product_number, name as product_name, weight, declared_value, declared_name_cn, declared_name_en', 'product_basic.id=purchase_order_item.product_basic_id')
				->joinLeft('product_warehouse', 'id as product_warehouse_id, actual_quantity as product_warehouse_actual_quantity, shelf_position, product_feature, tag', 'product_warehouse.product_basic_id=product_basic.id and product_warehouse.warehouse_id=' . $purchase_order['warehouse_id'])
				->joinLeft('product_material', 'id as product_material_id, number as material_number, name as material_name', 'product_material.id=product_warehouse.product_material_id')
				->order('product_basic.number asc')->asArray()->getAll();
			Helper_BSS_Normal::getProductImgUrl($item, 'product_number');
			$this->_view['purchase_order'] = $purchase_order;
			$this->_view['item'] = $item;
		}
	}
	
	function actionInfoEditSave()
	{
		$this->_view['title'] = '信息编辑（保存）';
		foreach ($_REQUEST['item'] as $k => $i)
		{
			$check_in_db = Product_Warehouse::find('id=?', $i['product_warehouse_id'])->getOne();
			if ($check_in_db['id'])
			{
				$check_in_db->product_material_id = $i['product_material_id'];
				$check_in_db->shelf_position = $i['shelf_position'];
				$check_in_db->product_feature = $i['product_feature'];
				$check_in_db->tag = $i['tag'];
				$check_in_db->save();
			}
		}
		return $this->_redirectMessage('保存成功', '正在返回..', url('Storage::Purchase/Detail', array('id'=>$_REQUEST['purchase_order_id'], 'show_all'=>0, 'operate'=>1)), SUCCESS);
	}
}